//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Files.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CA = ComponentArt.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.FileAdmin
{
	/// <summary>
	/// Summary description for CreateEditFolder.
	/// </summary>
	public class CreateEditFolder : BaseFilesAdminPage
	{
		#region Child Controls
		
		protected ResourceLinkButton SaveButton;
		protected Literal RegionTitle;
		protected FolderEditControl FolderDetails;
		protected ResourceControl RegionDescription;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.Controls.StatusMessage Status;
		
		#endregion

		private int sectionID;
		private CSContext context;
	
		private bool isNew
		{
			get{return sectionID <= 0;}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			SaveButton.Text = Components.ResourceManager.GetString("Save");
			
			context = CSContext.Current;
			sectionID = context.GetIntFromQueryString("SectionID", -1);
			
			if(isNew)
			{
				RegionTitle.Text = Components.ResourceManager.GetString("CP_FilesAdmin_CreateEditFolder_Title_New");
			}
			else
			{
				Folder f = CommunityServer.Files.Components.Folders.GetFolder(sectionID);
				RegionTitle.Text  = string.Format(ResourceManager.GetString("CP_FilesAdmin_CreateEditFolder_Title_Existing"), f.Name);
				SelectedNavigation1.SelectedNavItem="FilesFolders";
			}

			FolderDetails.SectionID = sectionID;

			ArrayList groups = CommunityServer.Files.Components.Folders.GetFolderGroups(false, true, false); 
			if (groups == null || groups.Count == 0)
			{
				Status.Success = false;
				Status.Text = CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_FilesAdmin_CreateEditFolder_NoFolderGroups");
				Status.Visible = true;

				FolderDetails.Visible = SaveButton.Visible = false;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		public void SaveButton_Click(object sender, EventArgs e)
		{
			if(!Page.IsValid)
				return;

			if (FolderDetails.Save() == null)
				return;			

			if(context.ReturnUrl != null && context.ReturnUrl.Length > 0)
				context.Context.Response.Redirect(context.ReturnUrl);
			else
				context.Context.Response.Redirect("Folders.aspx");
		}

	}
}
